<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Crypt;
class CenterController extends Controller
{

//=============个人信息开始===============

    //show 展示个人信息
    public function show(Request $request)
    {
        $conf = DB::table('bro_config') -> first();
        $link = DB::table('bro_link') -> orderBy('order', 'asc') -> get();
        //会话控制
        $user = $request->session()->get('user');
        $id = $user -> id;
        // dd($id);
        $data = DB::table('bro_user') -> where('id',$id) -> first();
        // dd($data);
    	$title = '我的京东';
    	return view('home.userinfo.basic',['title' => $title,'data' => $data,'user' => $user,'link' => $link,'conf' => $conf]);
    }

    //ajax
    // 判断邮箱
    public function email()
    {
        $eamil = $_POST['email'];
        $patten = '/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/';
        preg_match($patten,$eamil,$match);
        if($match)
        {
            echo 1;
        }else{
            echo 2;
        }
    }
    //判断手机 /^1[3|4|5|8] \d{9}$/
     public function phone()
    {
        $phone = $_POST['phone'];
        $patten = '/^1[3|4|5|8]\d{9}$/';
        preg_match($patten,$phone,$match);
        if($match)
        {
            echo 1;
        }else{
            echo 2;
        }
    }

    //更新基本信息

    public function update(Request $request)
    {
        $data = $request -> except('_token');
        // dd($data);
        // 发送修改语句
        $res = DB::table('bro_user') -> where('id',$data['id']) -> update($data);

        //判断修改是否成功
        if($res)
        {
            return redirect('/user') -> with('success','更新成功');
        }else{
            return back() -> with('error','更新失败');
        }
    }

//=============个人信息结束===============

//*****************************************分隔符***********************************************

//===========修改头像开始===================

    //upic 头像显示
    public function upic(Request $request)
    {
        $conf = DB::table('bro_config') -> first();
         $link = DB::table('bro_link') -> get();
          //会话控制
        $user = $request->session()->get('user');
        $id = $user -> id;
        $data = DB::table('bro_user') -> where('id',$id) -> first();
    	$title = '修改头像';
    	return view('home.userinfo.upic',['title' => $title,'data' => $data,'user' => $user,'link' => $link,'conf' => $conf]);
    }

    //上传头像处理

    public function upload(Request $request)
    {
        $user = $request->session()->get('user');
        $data = $request -> except('_token','oldpic');
        // dd($data);
        //处理pic地址,存入数据库
       $oldpath = '.'.$request -> input('oldpic');
       // dd($oldpath);
       if($request -> hasFile('pic'))//判断是否有文件
       {
            if($request -> file('pic') -> isValid())//判断文件是否有效
            {
                //获取文件的后缀名
                $suffix = $request -> file('pic') -> getClientOriginalExtension();
                // echo $suffix;
                //制作文件名
                $fileName = time().rand().'.'.$suffix;
                //上传文件
                $path = './uploads';//上传文件的目录

                //执行上传
                $request -> file('pic') -> move($path,$fileName);
                //拼凑pic的URL地址
                $data['pic'] = trim($path.'/'.$fileName,'.');
                
                $oldpic = './uploads/user/1.jpg';
                if(file_exists($oldpath))
                {
                    if($oldpath != $oldpic)
                    {
                      unlink($oldpath);
                    } 
                }

            }
       }
        // dd($data);
        // 发送修改语句
        $res = DB::table('bro_user') -> where('id',$data['id']) -> update($data);
        // dd($res);

        //判断修改是否成功
        if($res)
        {
            return redirect('/upic') -> with('success','更新成功');
        }else{
            return back() -> with('error','更新失败');
        }
    }
//===========修改头像结束=============================

//*********************************************分隔符*******************************************

//==================收货地址开始======================
    
    //添加收货地址
    public function address(Request $request)
    {
        $conf = DB::table('bro_config') -> first();
        $link = DB::table('bro_link') -> orderBy('order', 'asc') -> get();
        $title = '收货地址';
        $user = $request->session()->get('user');
        $id = $user -> id;
        $data = DB::table('bro_address') -> where('uid',$id) ->  get();
        // dd($data);
        return view('home.userinfo.address',['title' => $title,'data' => $data,'user' => $user,'link' => $link,'conf' => $conf]);
    }

    public function uphone()
    {
        $phone = $_POST['uphone'];
        $patten = '/^1[3|4|5|8]\d{9}$/';
        preg_match($patten,$phone,$match);
        if($match)
        {
            echo 1;
        }else{
            echo 2;
        }
    }


    //发送收货地址到数据库
    public function send()
    {
        // dd($_POST);
        $uid = $_POST['uid'];
        // echo $uid;
        $address = $_POST['address'];
        $utname = $_POST['utname'];
        $uphone = $_POST['uphone'];
        $post = $_POST['post'];

        $res = DB::table('bro_address')->insert(['uid' => $uid, 'address' => $address,'utname' => $utname,'uphone' => $uphone,'post' => $post]);
        if($res)
        {
            echo 1;
            exit;
        }else{
            echo 2;
            exit;
        }
    }

    //删除收货地址
    public function del($id)
    {
        // dd($pic);
        $res = DB::table('bro_address')-> where('id',$id) -> delete();

        //判断删除是否成功
       if($res)
        {
            return redirect('/address') -> with('success','删除成功');
        }else{
            return back() -> with('error','删除失败');
        }
    }

    //编辑收货地址
    public function edit(Request $request,$id)
    {
        $conf = DB::table('bro_config') -> first();
        $link = DB::table('bro_link') -> get();
        $title = "修改收货地址";
        //会话控制
        $user = $request->session()->get('user');
        $data = DB::table('bro_address') -> where('id',$id) -> first();
        return view('home.userinfo.edit',['title' => $title,'data' => $data,'user' => $user,'link' => $link,'conf' => $conf]);
    }

    public function change(Request $request)
    {
        $data = $request -> except('_token');

        // dd($data);
        // 发送修改语句
        $res = DB::table('bro_address') -> where('id',$data['id']) -> update($data);

        return redirect('/address');
    }
//=============收货地址结束=================

//*******************************************分隔符*********************************************

//=============我的订单开始=================
//订单列表
public function dingdan(Request $request)
{
    $title = "我的订单";
    $conf = DB::table('bro_config') -> first();
    $link = DB::table('bro_link') -> get();
    //会话控制
    $user = $request->session()->get('user');
    $uid = $user -> id;
    // dd($uid);
    $data = DB::table('bro_order') -> where('uid',$uid) -> get();
    return view('home.userinfo.dingdan',['title' => $title,'data' => $data,'conf' => $conf,'link' => $link,'user' => $user]);
}
//删除订单
public function delete($id)
{
    $res = $data = DB::table('bro_order') -> where('id',$id) -> delete();
    if($res)
    {
        return redirect('/dingdan');
    }
}


//订单商品详情
public function dingdan_detail(Request $request,$id)
{
    $title = "商品订单详情";
    $conf = DB::table('bro_config') -> first();
    $link = DB::table('bro_link') -> get();
    $data = DB::table('bro_indent') -> where('ordernum',$id) -> get();
    // dd($data -> );
    $dingdan = DB::table('bro_order') -> where('ordernum',$id) -> first();
    // dd($dingdan);
    //会话控制
    $user = $request->session()->get('user');
    return view('home.userinfo.dingdan_detail',['title' => $title,'conf' => $conf,'link' => $link,'user' => $user,'data' =>  $data,'dingdan' => $dingdan]);
}

//评价列表
public function commentDetail(Request $request,$id)
{
    $title = "评价列表";
    $conf = DB::table('bro_config') -> first();
    $link = DB::table('bro_link') -> get();
    $data = DB::table('bro_indent') -> where('ordernum',$id) -> get();
    // dd($data);
     //会话控制
    $user = $request->session()->get('user');
    foreach($data as $val)
    {

        $comment = DB::table('bro_comment') -> where('goods_id',$val -> goods_id) -> where('uid',$user -> id) -> get();
        // dd($comment);
    }
    // dd($data);
    return view('home.userinfo.comment_detail',['title' => $title,'data' => $data,'conf' => $conf,'link' => $link,'user' => $user,'comment' => $comment]);
}
//商品评论
public function comment(Request $request,$id)
{
    $title = "发表评价";
    $user = $request->session()->get('user');
    $conf = DB::table('bro_config') -> first();
    $link = DB::table('bro_link') -> get();
    $comment = DB::table('bro_indent') -> where('goods_id',$id) -> first() -> comment_status;
    // dd($comment);
    return view('home.userinfo.comment',['title' => $title,'user' => $user,'link' => $link,'conf' => $conf,'id' => $id,'comment' => $comment]);
}
//添加评论 更新订单商品详情商品评价状态
public function comment_up(Request $request)
{
    $data = $request -> except('_token','comment_status');
    // dd($data);
    // 将数据发送到数据库
    // $content = $request -> input('content');
    // // dd($content);
    // if($content == '')
    // {   
    //     $content = '没有填写评论';
    // }
    $goods_id = $request -> input('goods_id');
    // dd($goods_id);
    $res = DB::table('bro_comment') -> insert($data);
    if($res)
    {
        $update = DB::table('bro_indent') -> where('goods_id',$goods_id) -> update(['comment_status' => 1]);
        return redirect('/dingdan');
    }

}


// public function comment()
// {
//     $comment_status = $_POST['comment_status'];
//     // dd($comment_status);
//     $content = $_POST['content'];
//     if($content == '')
//     {   
//         $content = '没有填写评价';
//     }
//     $atime = $_POST['atime'];
//     $goods_id = $_POST['goods_id'];
//     $uid = $_POST['uid'];
//     $uname = $_POST['uname'];
//     $uip = $_POST['uip'];
//     $update = DB::table('bro_indent') -> where('goods_id',$goods_id) -> update(['comment_status' => $comment_status]);
//     $res = DB::table('bro_comment')->insert(['content' =>  $content, 'atime' =>  $atime,'goods_id' => $goods_id,'uid' => $uid,'uname' => $uname,'uip' => $uip]);
//     if($res)
//     {
//         echo 1;
//         exit;
//     }else{
//         echo 2;
//         exit;
//     }
// }



//=============我的订单结束=================

//=============修改密码开始================
public function changepass(Request $request)
{
    $title = "修改密码";
    $user = $request->session()->get('user');
    $conf = DB::table('bro_config') -> first();
    $link = DB::table('bro_link') -> get();
    return view('home.userinfo.changepass',['title' => $title,'user' => $user,'conf' => $conf,'link' => $link]);
}

public function pass(Request $request)
{
    $id = $request->session()->get('user') -> id;
    // $user = DB::table('bro_user') -> first();
    // $user = DB::table('bro_user') -> where('id',$id) ->  first();
    // dd($user);
    // dd($id);
    // $data = $request -> all();
    // dd($data);
     if($input = Input::all()){
        // dd($input);
        $rules = [
                'password'=>'required|between:6,20|confirmed',
            ];

            $message = [
                'password.required'=>'新密码不能为空！',
                'password.between'=>'新密码必须在6-20位之间！',
                'password.confirmed'=>'新密码和确认密码不一致！',
            ];
        $validator = Validator::make($input,$rules,$message);
        if($validator->passes()){
            $user = DB::table('bro_user') -> where('id',$id) ->  first();
            // dd($uid);
            $_password = Crypt::decrypt($user->password);
            if($input['password_o']==$_password){

                    $pass = Crypt::encrypt($input['password']);
                    // $user->update();
                    $res = DB::table('bro_user') -> where('id',$id) -> update(['password' => $pass]); 
                    return back()->with('errors','密码修改成功！');
                }else{
                    return back()->with('errors','原密码错误！');
                }
        }else{
                return back()->withErrors($validator);
        }

     }else{
            return view('home.userinfo.changepass');
    }
}











//=============修改密码结束================

}
